//
//  AppDelegate.m
//  PatientNew
//
//  Created by xxx on 14-7-10.
//  Copyright (c) 2014年 wtf. All rights reserved.
//

#import "AppDelegate.h"

#import "WTFRootTabBarController.h"

#include "../depends/include/sdkhelpers.h"


#import <AlipaySDK/AlipaySDK.h>
#import "MobClick.h"
#import "MainRootViewController.h"

#import "LeCloudPlayerService.h"
#import "WTFColors.h"

#import "WTFRootTabBarController.h"


#import "GetNickName.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //进入程序时调用
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    //shareSDK 初始化
    [ShareSDK registerApp:@"7ac5ff63e920"];//字符串api20为您的ShareSDK的AppKey
    
    //添加新浪微博应用 注册网址 http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"1256310662"
                               appSecret:@"445a1e7d2ff63c46774c59f2f1e7ce8e"
                             redirectUri:@"http://www.sharesdk.cn"];
    //当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台
    [ShareSDK  connectSinaWeiboWithAppKey:@"1256310662"
                                appSecret:@"445a1e7d2ff63c46774c59f2f1e7ce8e"
                              redirectUri:@"http://www.sharesdk.cn"
                              weiboSDKCls:[WeiboSDK class]];
    
    //添加腾讯微博应用 注册网址 http://dev.t.qq.com
    [ShareSDK connectTencentWeiboWithAppKey:@"1104588292"
                                  appSecret:@"CKqp12TMFUhteqTi"
                                redirectUri:@"http://www.sharesdk.cn"
                                   wbApiCls:[WeiboApi class]];
    
    //添加QQ空间应用  注册网址  http://connect.qq.com/intro/login/
    [ShareSDK connectQZoneWithAppKey:@"1104588292"
                           appSecret:@"CKqp12TMFUhteqTi"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    
    //添加QQ应用  注册网址  http://mobile.qq.com/api/
    [ShareSDK connectQQWithQZoneAppKey:@"1104588292"
                     qqApiInterfaceCls:[QQApiInterface class]
                       tencentOAuthCls:[TencentOAuth class]];
    
    //添加微信应用 注册网址 http://open.weixin.qq.com
    [ShareSDK connectWeChatWithAppId:@"wx1fcf5617831ce070"
                           wechatCls:[WXApi class]];
    //微信登陆的时候需要初始化
    [ShareSDK connectWeChatWithAppId:@"wx1fcf5617831ce070"
                           appSecret:@"88414f14f0dbc50bb5a45616941324b7"
                           wechatCls:[WXApi class]];
    
    
    
    //添加人人网应用 注册网址  http://dev.renren.com
    [ShareSDK connectRenRenWithAppId:@"476707"
                              appKey:@"1ba446d01b6a45628d24952911de245c"
                           appSecret:@"f0f97c5fba7c47f19addc8dbf2565e0a"
                   renrenClientClass:[RennClient class]];
    //
    //navigation
    //进入控制的 首页  根据判读是否是第一次启动
    [self savaDataByDefaults];
    //如果是第一次打开App启动引导图
    MainRootViewController* mainRootVC = [[MainRootViewController alloc]initWithNibName:@"MainRootViewController" bundle:nil];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults]setBool:true forKey:@"justWifi"];
        [[NSUserDefaults standardUserDefaults]setBool:true forKey:@"wifiControl"];
        [[NSUserDefaults standardUserDefaults]setObject:[GetNickName getNickName] forKey:@"nickName"];
        UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:mainRootVC];
        self.window.rootViewController = navi;
    }else{
        self.window.rootViewController = [WTFRootTabBarController sharedInstance];
    }
    //查看获取的 游客名称
    NSLog(@"----------%@---------------",[[NSUserDefaults standardUserDefaults]objectForKey:@"nickName"]);
    //友盟的初始化
    [MobClick startWithAppkey:@"5532051c67e58e982600259f" reportPolicy:BATCH   channelId:@" "];//channelId为nil或@""时，默认会被当作@"App Store"渠道。
    //yy sdk初始化，appName可修改成自己的，其他参数不需要修改,务必调用
    [[AppHelper sharedObject] initWithAppName:@"iosdemo" identifier:@"yym12ip" version:@"yym12ip9.0.0" protocolType:SdkProxyBProtocol];
    //乐视开启
    [LeCloudPlayerService startPlayerService];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    //如果极简开发包不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给开 发包
    if ([url.host isEqualToString:@"safepay"]) {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url
                                                  standbyCallback:^(NSDictionary *resultDic) {
                                                      NSLog(@"支付宝返回结果1result = %@",resultDic);
                                                  }]; }
    if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@" 支付宝返回结果2result = %@",resultDic);
        }];
    }
//    return YES;
    //shareSDK
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:self];
}

#pragma mark
#pragma mark - 根据Key 判断是不是第一次安装并打开app
-(void)savaDataByDefaults{
    //判断是否第一次打开
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched" ]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
}
//shareSDK
- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}

//- (BOOL)application:(UIApplication *)application
//            openURL:(NSURL *)url
//  sourceApplication:(NSString *)sourceApplication
//         annotation:(id)annotation
//{
//    return [ShareSDK handleOpenURL:url
//                 sourceApplication:sourceApplication
//                        annotation:annotation
//                        wxDelegate:self];
//}

@end
